/**
 * 
 */
package it.irondev.appturismo.util;

import it.irondev.appturismo.entity.City;
import it.irondev.appturismo.service.ICityService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.PreparerException;
import org.apache.tiles.preparer.ViewPreparer;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author federico
 *
 */

@Component
public class CityPanelPreparer implements ViewPreparer {

	ICityService cityService;


	public void execute(TilesRequestContext req, AttributeContext attributeContext)
			throws PreparerException {
 		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(((HttpServletRequest) req.getRequest()).getSession().getServletContext());
		cityService = ctx.getBean(ICityService.class);
		List<City> cities = cityService.getAllCity();
		attributeContext.putAttribute("menucities", new Attribute(cities));
		Integer favCity =  (Integer) ((HttpServletRequest) req.getRequest()).getSession().getAttribute("mapp-city");
		attributeContext.putAttribute("favouriteCity", new Attribute(favCity!=null ? favCity : -1 ));
	}

}
